package cao.oschina.function.other;

import android.content.Context;
import android.view.View;


import cao.oschina.api.Api;
import cao.oschina.app.AppContext;
import cao.oschina.app.Config;
import cao.oschina.bean.User;
import cao.oschina.widget.dialog.LoginDialog;
import hd.util.ToastUtil;
import hd.util.http.HttpResponse;


/**
 * 登录
 * <p/>
 * Created by Haidy on 15/4/6.
 */
public class LoginHelper {

    /**
     * 提醒登录
     *
     * @param onLoginResultListener 回调接口
     */
    public static void remindLogin(Context context, final OnLoginResultListener onLoginResultListener) {
        final LoginDialog dialog = new LoginDialog(context);
        dialog.addAction("取消", 1)
                .addAction("登录", 2)
                .setOnActionListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case 1:
                                onLoginResultListener.onLoginResult(false);
                                dialog.dismiss();
                                break;
                            case 2:
                                login(dialog, onLoginResultListener);
                                break;
                            default:
                                break;
                        }
                    }
                }).show();
    }

    /**
     * 登录
     *
     * @param dialog                Login Dialog
     * @param onLoginResultListener 回调接口
     */
    private static void login(final LoginDialog dialog, final OnLoginResultListener onLoginResultListener) {
        final String email = dialog.getEmail();
        final String pwd = dialog.getPwd();
        dialog.startLoading();
        Api.login(email, pwd, new Api.OnApiListener<HttpResponse>() {
            @Override
            public void result(boolean statue, HttpResponse data) {
                if (statue) {
                    User user = User.parser(data.text);
                    saveUserInfo((AppContext) dialog.getContext().getApplicationContext(), user, email, pwd, data.getCookie());
                    onLoginResultListener.onLoginResult(true);
                    dialog.dismiss();
                } else {
                    ToastUtil.toastShort(data.info);
                    dialog.stopLoading();
                }
            }
        });
    }

    /**
     * 保存个人信息
     *
     * @param user   user
     * @param email  email
     * @param pwd    密码
     * @param cookie cookie
     */
    private static void saveUserInfo(AppContext app, User user, String email, String pwd, String cookie) {
        Config config = app.getConfig();
        config.saveConfig("uid", user.uid);
        config.saveConfig("email", email);
        config.savePwd(pwd);
        config.saveConfig("cookies", cookie);
        config.saveConfig("location", user.location);
        config.saveConfig("name", user.name);
        config.saveConfig("followers", user.followers);
        config.saveConfig("fans", user.fans);
        config.saveConfig("score", user.score);
        config.saveConfig("favoritecount", user.favoritecount);
        config.saveConfig("gender", user.gender);
        if (user.portrait != null) {
            config.saveConfig("portrait", user.portrait);
        }

    }

    /**
     * 登录回调接口
     */
    public interface OnLoginResultListener {

        void onLoginResult(boolean result);
    }
}